
//启动	
init()

function init() {
	//设置字体响应
	setRem()
	
	////浏览次数上报
	cfg.accountInfo = storage.getItem("accountInfo")
	if(cfg.accountInfo){
		dataLog.body.event_name = 'page_view';
		dataLog.body.activity_account_id = cfg.accountInfo.account_id
		//dataLog.body.invited_status = _getQueryParam('invite_code') ? 1 : '';
		//dataLog.body.share_source_type = storage.getItem('sy_activity_login_code') || _getQueryParam('invite_code') ? 1 :'';
		SY_LOG.log(dataLog)
	}

}

	
function setRem(){
	//屏幕适应
		if (window.addEventListener) {
			var html = document.documentElement;
			function setFont() {
			  var html = document.documentElement;
			  var k = 750;
			  var multiple = (html.clientWidth / k) >1?1:(html.clientWidth / k);
			  html.style.fontSize = multiple * 100 + "px";
			}
			setFont();
			setTimeout(function() {
			  setFont();
			},
			100);
			document.addEventListener('DOMContentLoaded', setFont, false);
			window.addEventListener('resize', setFont, false);
			window.addEventListener('load', setFont, false);
		}
}	
function checkLogin(){
	var loginInfo = storage.getItem("loginInfo")
	var accountInfo = storage.getItem("accountInfo")
	if(!loginInfo){
		window.location.href = "./index.html"
		return
	}
	cfg.loginInfo = loginInfo
	cfg.accountInfo = accountInfo
}
function logOut(){
	//退出登录
	storage.setItem("accountInfo", "", storageCfg.timeOut)
	storage.setItem("loginInfo", "", storageCfg.timeOut)
	window.location.href = "./index.html"
}

function getAccountInfo(option) {
	//获取活动账号信息   在获取账号信息后，再进行相关业务
	var params = {}
	params["login_type"] = option.login_type;
	if (option.login_type == 1) {
		params["token"] = option.token;
	} else if (option.login_type == 2) {
		params["role_code"] = option.role_code;
	} else if (option.login_type == 3) {
		params["captcha_code"] = option.captcha_code;
		params["phone_number"] = option.phone_number;
	}
	
	params["act_id"] = cfg.act_id;
	//params["invite_code"] = _getQueryParam("invite_code");
	//params["invite_type"] = _getQueryParam("invite_type");
	params["ts"] = Math.round(new Date() / 1000);
	params["sign"] = sign(params, cfg.secret_active);


	getApi({url:cfg.api_active+"/activity/getAccountInfo",type:"POST",data:params}).then(res => {
		if(res.code===0){
			cfg.accountInfo = res.data
			dataLog.body.activity_account_id = res.data.account_id
			storage.setItem("accountInfo", res.data, storageCfg.timeOut)
			mui.toast("登录成功！")
			setTimeout(function(){
				if(res.data.role_id){
					//已经绑定角色
					window.location.href ="./haoyou.html"
				}else{
					window.location.href ="./bdjs.html"
				}
			},500)
		}
	})

}
//发送验证码
function sendSMS(phone) {
	// 短信验证码发送
	var params = {}
	params["sms_type"] = "phone_login";
	params["phone_number"] = phone || 1111;
	params["ticket"] = Math.round(new Date() / 1000);
	params["rand_str"] = Math.round(new Date() / 1000);
	params["ts"] = Math.round(new Date() / 1000);
	params["sign"] = sign(params, cfg.secret);
	$.ajax({
		url: cfg.API + "/sms/send",
		type: 'GET',
		data: params,
		dataType: 'json',
		success: function(data, textStatus, jqXHR) {
			// 请求成功时的回调函数
			console.log("成功获取数据: ", data);
		},
		error: function(jqXHR, textStatus, errorThrown) {
			// 请求失败时的回调函数
			console.log("请求失败: ", textStatus, errorThrown);
		}
	});
}
var _timmer;
var _timmer_count =storage.getItem("_timmer_count") || 0;

var storageCfg={
	timeOut:60*60*1000
}
function codeSend(phone) {
	if(_timmer ||  _timmer_count- _maxTime>0){
		mui.toast("请" + (_maxTime - _timmer_count) + "秒后重试！");
		return
	}
	
	if(_timmer_count==0){
		sendSMS(phone)
		mui.toast("发送验证成功！");
		_timmer = setInterval(() => {
			if (_timmer_count > _maxTime) {
				_timmer_count = 0;
				storage.setItem("timmer_count", _timmer_count, storageCfg.timeOut)
				clearInterval(_timmer);
				_timmer=""
				return
			}
			_timmer_count++;
			storage.setItem("timmer_count", _timmer_count, storageCfg.timeOut)
		}, 1000)
		
	}
}


// 接口
function getApi(options) {
      var jqxhr = $.ajax({
        url: options.url,
        type: options.type || 'POST',
        data: options.data || {},
        dataType: options.dataType || 'json',
      }).done(function(res){
		 if(res.code !==0){
			 mui.toast(res.message);
		 }
	  }).fail(function () {
        let messageItem = '系统繁忙，请稍候再试~';
		mui.toast(messageItem);
      })
      return jqxhr
}



function phoneLogin(phone_number, code) {
	//手机验证码登录
	var params = {}
	params["phone_number"] = phone_number;
	params["code"] = code;
	params["ts"] =Math.round(new Date() / 1000);
	params["sign"] = sign(params, cfg.secret_sy);
	
	getApi({url:cfg.api_sy+"/web/phoneLogin",data:params}).then(res => {
		console.log(res);
		getAccountInfo({
			login_type: 3,
			captcha_code: params.code,
			phone_number: params.phone_number
		})
	})
}

function LoginPwd(phone_number, password) {
	//手机用户登录接口
	var params = {}
	params["phone_number"] = phone_number;
	params["password"] = password;
	params["ts"] = Math.round(new Date() / 1000);
	params["sign"] = sign(params, cfg.secret_sy);
	
	getApi({url:cfg.api_sy+"/web/login",data:params}).then(res=>{
		cfg.loginInfo = res
		console.log(res);
	})

}

function loginCode(jsmCode) {
	//角色登录码
	var params = {}
	params["jsmCode"] = jsmCode;

	params["ts"] = Math.round(new Date() / 1000);
	params["sign"] = sign(params, cfg.secret);
}


/*
工具类
*/
function _getQueryParam(param) {
	const urlParams = new URLSearchParams(window.location.search);
	return urlParams.get(param);
}